<template>
  <div id="AsnDetailList">
    <v-tableTitle
      :btnConfig="btnConfig"
      :tableSelectData="selectData"
      @backList="backList"
      @del="del"
      @newCreate="newCreate"
      @orderPick="orderPick"
      @deliver="deliver"
    ></v-tableTitle>
    <div class="detail">
      <v-navMenu
        :detailNav="detailNav"
        :menuData="menuData"
        @setMentActive="setMentActive"
        v-if="detailNav != 1"
      ></v-navMenu>
      <component
        :data="{ detailData, argument }"
        :is="currentComponent"
      ></component>
    </div>
  </div>
</template>

<script>
import tableTitle from "commons/TableTitle";
import navMenu from "commons/NavMenu";
import baseMixin from "@/mixins/BaseMixin";
import config from "../Config";

export default {
  name: "DetailList",
  mixins: [baseMixin, config],
  data() {
    return {
      newFlag: false,
      argument: {},
      detailData: {},
      selectData: [],
      menuData: [
        { label: this.$t("out.sodetail", { no: 1 }), index: "2" },
        { label: this.$t("out.sodetail", { no: 2 }), index: "3" },
        { label: this.$t("config.batchProp"), index: "4" },
        { label: this.$t("out.pickdesc"), index: "5" },
      ],
      detailNav: "1",
      componentConfig: [
        {
          name: "Table",
          label: "table",
          component: () => import("./page/Table.vue"),
        },
        {
          name: "OrderdetailSO1",
          label: this.$t("out.sodetail", { no: 1 }),
          component: () => import("./page/OrderdetailSO1.vue"),
        },
        {
          name: "OrderdetailSO2",
          label: this.$t("out.sodetail", { no: 2 }),
          component: () => import("./page/OrderdetailSO2.vue"),
        },
        {
          name: "OrderdetailLottable",
          label: this.$t("config.batchProp"),
          component: () => import("./page/OrderdetailLottable.vue"),
        },
        {
          name: "OrderdetailPick",
          label: this.$t("out.pickdesc"),
          component: () => import("./page/OrderdetailPick.vue"),
        },
      ],
    };
  },
  computed: {
    btnConfig() {
      if (this.detailNav == 1) {
        return [
          /*{label: this.$t('common.create'), certigier: this.newDetailStatus, invalid: "newCreate"},
                        {label: this.$t('common.delete'), certigier: this.deleteDetailStatus, disabled: true, invalid: "del"},*/
          {
            type: "more",
            label: this.$t("out.action"),
            certigier: !this.btnPermNameList.includes('WM020417'),
            invalid: "down",
            btnConfig: [
              {
                label: this.$t("out.pick"),
                certigier: this.pickingStatus||!this.btnPermNameList.includes('WM020419'),
                disabled: true,
                invalid: "orderPick",
              },
              {
                label: this.$t("config.deliver"),
                certigier: this.deliverDetailStatus||!this.btnPermNameList.includes('WM020420'),
                disabled: true,
                invalid: "deliver",
              },
            ],
          },
        ];
      } else if (this.$store.state.newFlag) {
        return [];
      } else {
        return [{ label: this.$t("common.return"),certigier: !this.btnPermNameList.includes('WM020418'),  invalid: "backList" }];
      }
    },
    currentComponent() {
      return this.componentConfig[this.detailNav - 1].component;
    },
  },
  methods: {
    deliver(data) {
      data.forEach(async (item, index) => {
        try {
          let res = await this.$api.post(`/getDeliverPickdetailkeyBySku`, item);
          
            this.$message.success(res.returnMgr);
            let page = sessionStorage.getItem("page");
            this.$children[1].getTableData(page, {});
        } catch (e) {
          this.$message.error(e);
        } finally {
          this.$children[1].$refs.table.delSelectData(item, index);
        }
      });
    },
    orderPick(data) {
      data.forEach(async (item, index) => {
        try {
          let res = await this.$api.post(`/getPickdetailListByPicking`, item);
          
            this.$message.success(res.returnMgr);
            let page = sessionStorage.getItem("page");
            this.$children[1].getTableData(page, {});
        } catch (e) {
          this.$message.error(e);
        } finally {
          this.$children[1].$refs.table.delSelectData(item, index);
        }
      });
    },
    //删除数据
    async del(data) {
      try {
        let res = await this.$api.post(`/deleteOrdersDetail`, data);
        
          this.$message.success(res.returnMgr);
          let page = sessionStorage.getItem("page");
          this.$children[1].getTableData(1, {});
        
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.$children[1].$refs.table.delSelectData(data);
      }
    },
    //选中的数据
    setSelectData(data) {
      this.selectData = data;
    },
    //进入详情
    async setdialog(data) {
      try {
        let res = await this.$api.post(`/queryOrderdetailByOrderlinenumber`, {
          orderkey: this.argument.orderkey,
          orderlinenumber: data.orderlinenumber,
        });
        
          this.detailData = res.bean;
          this.detailNav = "2";
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //返回试图
    backList() {
      this.detailNav = "1";
      this.$store.commit("setDetailsNew", false);
    },
    //新建
    async newCreate() {
      this.detailData = {
        adjustedqty: 0,
        allocatestrategytype: "1",
        altsku: " ",
        cartonbreak: " ",
        cartongroup: " ",
        convertedqty: 0,
        cubicmeter: 0,
        extendedprice: 0,
        externlineno: "",
        externorderkey: " ",
        facility: " ",
        forte_flag: "I",
        fulfillqty: 0,
        generatecontainerdetail: "0",
        hundredweight: 0,
        id: " ",
        issubstitute: 0,
        lot: " ",
        lottable01: " ",
        lottable02: " ",
        lottable03: " ",
        lottable06: " ",
        lottable07: " ",
        lottable08: " ",
        lottable09: " ",
        lottable10: " ",
        manufacturersku: " ",
        minshippercent: 0,
        oktosubstitute: 0,
        openqty: 0,
        opprequest: "0",
        oqcautoadjust: "0",
        oqcrequired: "0",
        orderlinenumber: "",
        origcaseqty: 0,
        originalqty: 0,
        origlayerqty: 0,
        origpalletqty: 0,
        packkey: "STD",
        pallet_id: " ",
        pickcode: " ",
        product_cube: 0,
        product_weight: 0,
        qtyallocated: 0,
        qtyintransit: 0,
        qtypicked: 0,
        qtypreallocated: 0,
        qtytoprocess: 1,
        retailsku: " ",
        sequence: 0,
        shelflife: 0,
        shipgroup01: "N",
        shipgroup02: "N",
        shipgroup03: "N",
        shippedqty: 0,
        sku: "",
        skurotation: "Lot",
        status: "04",
        storerkey: "",
        sub_flag: " ",
        tariffkey: "XXXXXXXXXX",
        tax01: 0,
        tax02: 0,
        unitprice: 0,
        uom: "EA",
        updatesource: "0",
        workorderkey: " ",
        wpreleased: "0",
      };
      try {
        let res = await this.$api.get(
          `/queryOrderdetailMaxLine/${this.argument.orderkey}`
        );
        
          this.$store.commit("setDetailsNew", true);
          this.detailData.orderlinenumber = res.bean;
          this.detailData.externlineno = "WMS" + res.bean;
          this.detailData.orderkey = this.selectData.orderkey;
          this.detailNav = "2";
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //导航值
    setMentActive(key) {
      this.detailNav = key;
    },
  },
  mounted() {
    this.newFlag = this.$store.state.newFlag;
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.argument = data.routeData;
      this.detailData = data.detailList;
      this.detailNav = data.detailListNav;
    } else if (this.newFlag) {
      this.detailData = {
        adjustedqty: 0,
        allocatestrategytype: "1",
        altsku: " ",
        cartonbreak: " ",
        cartongroup: " ",
        convertedqty: 0,
        cubicmeter: 0,
        extendedprice: 0,
        externlineno: "WMS00001",
        externorderkey: " ",
        facility: " ",
        forte_flag: "I",
        fulfillqty: 0,
        generatecontainerdetail: "0",
        hundredweight: 0,
        id: " ",
        issubstitute: 0,
        lot: " ",
        lottable01: " ",
        lottable02: " ",
        lottable03: " ",
        lottable06: " ",
        lottable07: " ",
        lottable08: " ",
        lottable09: " ",
        lottable10: " ",
        manufacturersku: " ",
        minshippercent: 0,
        oktosubstitute: 0,
        openqty: 0,
        opprequest: "0",
        oqcautoadjust: "0",
        oqcrequired: "0",
        orderlinenumber: "00001",
        origcaseqty: 0,
        originalqty: 0,
        origlayerqty: 0,
        origpalletqty: 0,
        packkey: "STD",
        pallet_id: " ",
        pickcode: " ",
        product_cube: 0,
        product_weight: 0,
        qtyallocated: 0,
        qtyintransit: 0,
        qtypicked: 0,
        qtypreallocated: 0,
        qtytoprocess: 1,
        retailsku: " ",
        sequence: 0,
        shelflife: 0,
        shipgroup01: "N",
        shipgroup02: "N",
        shipgroup03: "N",
        shippedqty: 0,
        sku: "",
        skurotation: "Lot",
        status: "04",
        storerkey: "",
        sub_flag: " ",
        tariffkey: "XXXXXXXXXX",
        tax01: 0,
        tax02: 0,
        unitprice: 0,
        uom: "EA",
        updatesource: "0",
        workorderkey: " ",
        wpreleased: "0",
        orderkey: this.$store.state.routeId,
      };
      this.detailNav = "2";
    } else {
      this.argument = this.$store.state.routeData;
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-navMenu": navMenu,
  },
  watch: {
    detailNav: {
      handler(data) {
        if (data != "1") {
          this.$store.commit("setTemporary", true);
        } else {
          this.$store.commit("setTemporary", false);
        }
      },
      immediate: true,
      deep: true,
    },
    selectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteDetailStatus = this.pickingStatus = this.deliverDetailStatus = false;
          this.deliver2Status = false;
          this.selectDetailValue = event;
        } else {
          this.deliver2Status = true;
          this.deleteDetailStatus = this.pickingStatus = this.deliverDetailStatus = true;
        }
      }
    },
  },
};
</script>

<style></style>
